   	// global variable to store the variable values of each url
	var time_score = new Array(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);	
	var traffic_score = new Array(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
	var quality_score = new Array(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
	var pageview_score = new Array(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
	var speed_score = new Array(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
	var final_score = new Array();
	var query;
	var categories;
	var speed = 5000;
	
	// Read a page's GET URL variables and return them as an associative array.
	function getUrlVars(){
	    var vars = [], hash;
	    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
	    for(var i = 0; i < hashes.length; i++){
	        hash = hashes[i].split('=');
    	    vars.push(hash[0]);
       		vars[hash[0]] = hash[1];
    	}
    	return vars;
	}
   	
   	
   	function load(){
		if(getUrlVars()['q']!=""){
			query = getUrlVars()['q'];

			$("#q").val(query);

			load_time();
			load_speed();
			load_category();
			load_popularity();
			load_traffic();
			load_keyword();
		}	
			
	}  
   
	// 
	function show_ranking(){
		customize_ranking();
		display_result();
	}
   
  
   
   	
	// customize ranking function
	function customize_ranking(){
		var time = $( "#time" ).val();
		var quality = $( "#quality" ).val();
		var speed = $( "#speed" ).val();
		var traffic = $( "#traffic" ).val();
		var keyword = $("#keyword").val();
		//$("#debug").append("<br><br>time: "+speed+" speed score: "+ speed_score.toString());
		//$("#debug").append("<br>traffic: "+traffic+" traffic score: "+ traffic_score.toString());
		//$("#debug").append("<br>quality: "+quality+" quality score: "+ quality_score.toString());
		//$("#debug").append("<br>quality: "+quality+" pageview score: "+ pageview_score.toString());
		//$("#debug").append("<br>time: "+time+" time score: "+ time_score.toString());
		$("#debug").append("<br>final score: ");
		for(var i=0;i<24;i++){
			// Need update to new ranking method later
			var obj = new Object();
			obj.id = i;
			obj.score = keyword*(1-i/24) + time * time_score[i] + 0.5*quality * quality_score[i] + 0.5*quality * pageview_score[i] + speed * speed_score[i] + traffic * traffic_score[i]; 
			final_score[i] = obj;
			$("#debug").append(" id: "+final_score[i].id+" score: "+final_score[i].score+"<br>");
			
		}

	}
	
	// display new ranking result
	function display_result(){
		var inverted_array = final_score;
		inverted_array.sort(scoreCompare);
		var output="";
		if(!$("#category").is(':checked')){
			for(var i=inverted_array.length-1; i>=0; i--){
				var id=inverted_array[i].id;
				//$("#debug").append(" <br>order result<br>id: "+id+" score: "+inverted_array[i].score+" ");
				output = output 
						+ "<li id='"+id+"' style=\"background-image:url(images/arrow.gif); background-repeat:no-repeat; background-position:10px 7px; padding-left:30px;\">"
						+ $("#"+id).html()
						+ "</li>";
			}
			$("#ranked_result").html(output);
		}
		else{
			var str="";
			for(var i=0;i<categories.length;i++){
				var temp = categories[i];
				$("#debug").append("<br>category: "+ categories[i].url_list);
				
				str = str + "<div width='100%' style='border: 1px dotted lightgrey;margin-bottom:20px;margin-left:10px;'><div style='background-color:#F8F8F8;margin-bottom:15px;'><b style='font-size:18px;margin-left:10px;'>";
				var name = temp.category_name;
				for(var w=1; w<=name.length; w++){
					if(w%80==0) str = str + "</b><br/><b style='font-size:18px;margin-left:10px;'>";
					str = str + name.charAt(w-1);
				}
				str = str +"</div></b>";
				
				for (var j=inverted_array.length-1; j>=0; j--){
					var id=inverted_array[j].id;
					if(in_array(temp.url_list,id)){
						str = str	
							+ "<li id='"+id+"' style=\"background-image:url(images/arrow.gif); background-repeat:no-repeat; background-position:10px 7px; padding-left:30px;\">"
							+ $("#"+id).html()
							+ "</li>";
						}				
				}
				str = str + "</div>";
			}
			$("#ranked_result").html(str);
		}

	}
	
	function scoreCompare(a,b){
		return a.score - b.score;
	}
	
	function in_array(arr, value){
		for(var i=0; i<arr.length;i++){
			if(arr[i]==value)
				return true;
		}
		return false;
	}
	

	
	function load_time(){	
		var str = "q="+query+"&response_group=SiteData";
		//Loading site data, used for created time
		$.ajax({
			url: "libraries/calculate_score.php",
			type: "GET",
			dataType:"json",
			data: str,
			timeout: 60000,
			error:function (xhr, ajaxOptions, thrownError){
                 setTimeout("load_time()", speed);
                 $("#debug").append("time "+xhr.status+" "+ thrownError+"<br><br>");
            }, 
			success: function(data){
				$( "#time_slider" ).innerHTML="";
				time_score = data.result;
				$( "#time_slider" ).slider({
					range: "min",
					value: 0,
					min: 0,
					max: 10,
					slide: function( event, ui ) {
						$( "#time" ).val(ui.value );
						show_ranking();
				}
			});
			$( "#time" ).val( $( "#time_slider" ).slider( "value" ) );
			}
		});
	}
   
   
   
   	function load_keyword(){
   		$( "#keyword_slider" ).slider({
			range: "min",
			value: 10,
			min: 0,
			max: 10,
			slide: function( event, ui ) {
				$( "#keyword" ).val(ui.value );
				show_ranking();
			}
		});
		$( "#keyword" ).val( $( "#keyword_slider" ).slider( "value" ) );
   	}
   
   
   	function load_speed(){
		// Loading Speed Info
		str = "q="+query+"&response_group=Speed";
		$.ajax({
			url: "libraries/calculate_score.php",
			type: "GET",
			dataType:"json",
			data: str,
			timeout: 60000,
			error:function (xhr, ajaxOptions, thrownError){
				  setTimeout("load_speed()", speed);
                  $("#debug").append("speed "+xhr.status+" "+ thrownError+"<br><br>");
            }, 
			success: function(data){
				$( "#speed_slider" ).text="";
				speed_score = data.result;
				$( "#speed_slider" ).slider({
				range: "min",
				value: 0,
				min: 0,
				max: 10,
				slide: function( event, ui ) {
					$( "#speed" ).val(ui.value );
					show_ranking();
				}
			});
			$( "#speed" ).val( $( "#speed_slider" ).slider( "value" ) );
			}
		});
	}

   
   	// Loading Categories Info
	function load_category(){
		$.ajax({
			url: "libraries/show_category.php",
			type: "GET",
			dataType: "json",
			data: "q="+query,
			timeout: 60000,
			error:function (xhr, ajaxOptions, thrownError){
				  setTimeout("load_category()", speed);
                  $("#debug").append("category "+xhr.status+" "+ thrownError+"<br><br>");
            }, 
			success: function(data){
				categories = data;
				$("#show_category").css('display','');
			}
		});
	}
   
   
   
   	function load_popularity(){
		//Loading LinksInCount, used for popularity
		$.ajax({
			url: "libraries/calculate_score.php",
			type: "GET",
			dataType:"json",
			data: "q="+query+"&response_group=LinksInCount",
			timeout: 60000,
			error:function (xhr, ajaxOptions, thrownError){
                  setTimeout("load_popularity()", speed);
                  $("#debug").append("popularity "+xhr.status+" "+ thrownError+"<br><br>");
            }, 
			success: function(data){
		   		$( "#quality_slider" ).text="";
		   		set_quality_score(data);
		   		$( "#quality_slider" ).slider({
					range: "min",
					value: 0,
					min: 0,
					max: 10,
				slide: function( event, ui ) {
					$( "#quality" ).val(ui.value );
					show_ranking();
				}
			});
			$( "#quality" ).val( $( "#quality_slider" ).slider( "value" ) );
			}
		});
	}
	
	
	function set_quality_score(data){
		quality_score = data.result;
	}
	
   
   
   
   function load_traffic(){	
		// Loading Traffic Data
		$.ajax({
			url: "libraries/calculate_score.php",
			type: "GET",
			dataType: "json",
			data: "q="+query+"&response_group=TrafficData",
			timeout: 60000,
			error:function (xhr, ajaxOptions, thrownError){
				  setTimeout("load_traffic()", speed);
                  $("#debug").append("traffic "+xhr.status+" "+ thrownError+"<br><br>");
            }, 
			success: function(data){
				$( "#traffic_slider" ).text="";
				set_traffic_score(data);
				$( "#traffic_slider" ).slider({
				range: "min",
				value: 0,
				min: 0,
				max: 10,
				slide: function( event, ui ) {
					$( "#traffic" ).val(ui.value );
					show_ranking();
				}
			});
			$( "#traffic" ).val( $( "#traffic_slider" ).slider( "value" ) );
			}
		});
	}
	
	
	
	function set_traffic_score(data){
		switch($( "#time" ).val()){
			case 5:
				traffic_score = data.result[3].traffic_score;
				pageview_score = data.result[3].quality_score;
				break;
			case 4:
				traffic_score = data.result[3].traffic_score;
				pageview_score = data.result[3].quality_score;
				break;
			case 3:
				traffic_score = data.result[2].traffic_score;
				pageview_score = data.result[2].quality_score;
				break;
			case 2:
				traffic_score = data.result[1].traffic_score;
				pageview_score = data.result[1].quality_score;
				break;
			case 1:
				traffic_score = data.result[0].traffic_score;
				pageview_score = data.result[0].quality_score;
				break;
			case 0:
				traffic_score = data.result[0].traffic_score;
				pageview_score = data.result[0].quality_score;
				break;	
			default: 
				traffic_score = data.result[0].traffic_score;
				pageview_score = data.result[0].quality_score;
				break;	
		}	
	}
